package com.admin.education.services.Implements;

import com.admin.education.common.exceptions.ResourceNotFoundException;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.activity.ActivityTransfer;
import com.admin.education.dataTransferObject.activity.Mapper.ActivityMapper;
import com.admin.education.dataTransferObject.activity.Request.CreateActivityRequest;
import com.admin.education.dataTransferObject.activity.Request.UpdateActivityRequest;
import com.admin.education.dataTransferObject.userAnswerSheet.Mapper.UserAnswerSheetMapper;
import com.admin.education.dataTransferObject.userAnswerSheet.UserAnswerSheetTransfer;
import com.admin.education.models.Activities;
import com.admin.education.models.QActivities;
import com.admin.education.models.UserAnswerSheet;
import com.admin.education.repositories.ActivitiesRepository;
import com.admin.education.services.Interfaces.IActivityService;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Validated
public class ActivityService implements IActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivitiesRepository activitiesRepository;
    private QActivities qActivities;
    public  ActivityService(){
        qActivities= QActivities.activities;
    }
    @Override
    public ServicesExcuteResult<PageResponse<ActivityTransfer>> queryActivities(@Valid PageRequest pageRequest) {
        ServicesExcuteResult<PageResponse<ActivityTransfer>> servicesExcuteResult=new ServicesExcuteResult<>();

        JPAQuery<Activities> activitiesJPAQuery = this.activitiesRepository.selectFrom(qActivities);

        long totalCount = activitiesJPAQuery.fetchCount();

        activitiesJPAQuery = activitiesJPAQuery.orderBy(qActivities.beginTime.desc()).offset((pageRequest.getPageIndex()-1)*pageRequest.getPageSize()).limit(pageRequest.getPageSize());

        List<Activities> activities= activitiesJPAQuery.fetch();

        List<ActivityTransfer> activityTransfers = activityMapper.mapFrom(activities);

        PageResponse<ActivityTransfer> activityTransferPageResponse = new PageResponse<>(pageRequest.getPageSize(),pageRequest.getPageIndex(),(int)totalCount,activityTransfers);

        servicesExcuteResult.setData(activityTransferPageResponse);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult createActivity(CreateActivityRequest createActivityRequest) {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        Activities activities =activityMapper.mapFrom(createActivityRequest);
        activities.setCreateTime(new Date());
        activities.setValid(true);

        this.activitiesRepository.persist(activities);

        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult updateActivity(long activityId, @Valid UpdateActivityRequest activityRequest) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Activities> activitiesJPAQuery = this.activitiesRepository.selectFrom(qActivities).where(qActivities.id.eq(activityId));
        if(0!=activitiesJPAQuery.fetchCount()){
            Activities activities = activitiesJPAQuery.fetchFirst();

            activities = activityMapper.mapFrom(activityRequest,activities);

            this.activitiesRepository.merge(activities);

            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("活动不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult deleteActivity(long activityId) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();
        JPAQuery<Activities> activitiesJPAQuery = this.activitiesRepository.selectFrom(qActivities).where(qActivities.id.eq(activityId));
        if(0!=activitiesJPAQuery.fetchCount()){
            Activities activities = activitiesJPAQuery.fetchFirst();

            if(!activities.isValid()|| activities.getEndTime().compareTo(new Date())<0){

                this.activitiesRepository.remove(activities);

                servicesExcuteResult.setSuccess(true);
            }
            else
            {
                servicesExcuteResult.setErrorMessage("活动不能删除");
            }
        }
        else {
            throw new ResourceNotFoundException("活动不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult setValudActivity(long activityId) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Activities> activitiesJPAQuery = this.activitiesRepository.selectFrom(qActivities).where(qActivities.id.eq(activityId));
        if(0!=activitiesJPAQuery.fetchCount()){
            Activities activities = activitiesJPAQuery.fetchFirst();

            activities.setValid(!activities.isValid());

            this.activitiesRepository.merge(activities);
            servicesExcuteResult.setSuccess(true);
        }
        else{
            throw new ResourceNotFoundException("活动不存在");
        }
        return  servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<ActivityTransfer> queryActivityById(long activityId) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Activities> activitiesJPAQuery = this.activitiesRepository.selectFrom(qActivities).where(qActivities.id.eq(activityId));
        if (0 != activitiesJPAQuery.fetchCount()) {
            Activities activities = activitiesJPAQuery.fetchFirst();

            servicesExcuteResult.setData(activityMapper.mapFrom(activities));
            servicesExcuteResult.setSuccess(true);
        } else {
            throw new ResourceNotFoundException("活动不存在");
        }
        return servicesExcuteResult;
    }
}
